/**
 * @file    app_thigh.c
 * @author  yhy
 * @brief   腿部系统模块源文件
 * @version 1.00
 * @date    2025-07-30
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "app_thigh.h"

#include "common_debug.h"
#include "inf_pump.h"

static void     app_thigh_apply_mode(airbag_system_t *system, uint32_t mode);
static result_e app_thigh_validate_mode(uint32_t mode);

static airbag_system_t thigh_system;
static airbag_t        thigh_airbags[THIGH_AIRBAG_IDX_MAX];
static cps121_idx_e    thigh_sensor_mapping[THIGH_AIRBAG_IDX_MAX];

/* 阀门通道配置 */
static airbag_valve_config_t thigh_valve_configs[THIGH_AIRBAG_IDX_MAX] = {
    [THIGH_AIRBAG_IDX_0] = {
        .inflate_valve_ch = SOLENOID_VALVE_CH10, // YT1
        .deflate_valve_ch = SOLENOID_VALVE_CH11, // YTR1
        .pressure_sensor_idx = CPS121_IDX_8,
    },
};

/* 腿部系统配置 */
static const airbag_system_config_t thigh_config = {
    .max_inflate_time_ms    = 60 * 1000,
    .dispatching_cycle_ms   = 50,
    .airbag_count           = THIGH_AIRBAG_IDX_MAX,
    .valve_configs          = thigh_valve_configs,
    .air_pressure_threshold = 165, /* 气压保护阈值 165 KPa */
};

/* 回调函数集合 */
static const airbag_system_callbacks_t thigh_callbacks = {
    .apply_mode    = app_thigh_apply_mode,
    .validate_mode = app_thigh_validate_mode,
};

/**
 * @brief 初始化腿部系统
 *
 * @return result_e 初始化结果, 初始化成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_thigh_init(void)
{
    thigh_system.airbags        = thigh_airbags;
    thigh_system.sensor_mapping = thigh_sensor_mapping;
    return app_airbag_system_init(&thigh_system, &thigh_config, AIRBAG_SYSTEM_TYPE_THIGH, &thigh_callbacks);
}

/**
 * @brief 设置腿部系统运行模式
 *
 * @param   mode        运行模式
 * @return  result_e    设置结果, 设置成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_thigh_set_mode(thigh_mode_e mode)
{
    return app_airbag_system_set_mode(&thigh_system, (uint32_t)mode);
}

/**
 * @brief 停止腿部系统运行
 *
 * @return result_e 停止结果, 停止成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_thigh_stop(void) { return app_airbag_system_stop(&thigh_system); }

/**
 * @brief 处理腿部系统运行状态, 在调度器中循环调度
 *
 */
void app_thigh_handle(void) { app_airbag_system_handle(&thigh_system); }

/**
 * @brief 获取当前腿部系统运行模式
 *
 * @return thigh_mode_e 当前运行模式
 */
thigh_mode_e app_thigh_get_mode(void) { return (thigh_mode_e)app_airbag_system_get_mode(&thigh_system); }

/**
 * @brief 获取指定腿部系统气袋(支撑)状态
 *
 * @param   airbag_idx      气袋索引
 * @return  airbag_state_e  气袋支撑状态
 */
airbag_state_e app_thigh_get_airbag_state(thigh_airbag_idx_e airbag_idx)
{
    return app_airbag_system_get_airbag_state(&thigh_system, (uint32_t)airbag_idx);
}

/**
 * @brief 获取指定腿部系统气袋压力值
 *
 * @param airbag_idx    气袋索引
 * @return uint32_t     气袋压力值, 单位为KPa, 0表示未获取到
 */
uint32_t app_thigh_get_air_pressure(thigh_airbag_idx_e airbag_idx)
{
    return app_airbag_system_get_air_pressure(&thigh_system, (uint32_t)airbag_idx);
}

/**
 * @brief 应用腿部系统运行模式
 *
 * @param system    腿部系统指针
 * @param mode      运行模式
 */
static void app_thigh_apply_mode(airbag_system_t *system, uint32_t mode)
{
    thigh_mode_e thigh_mode = (thigh_mode_e)mode;

    switch (thigh_mode) {
    case THIGH_MODE_UP: // up 充气
        // inf_pump_air_inflation();
        app_airbag_system_ctrl_airbag(system, THIGH_AIRBAG_IDX_0, AIRBAG_STATE_INFLATE);
        break;
    case THIGH_MODE_DOWN: // down 抽气
        // inf_pump_air_exhaust();
        app_airbag_system_ctrl_airbag(system, THIGH_AIRBAG_IDX_0, AIRBAG_STATE_EXHAUST);
        break;
    case THIGH_MODE_KEEP: // keep 保压
        // inf_pump_air_stop();
        app_airbag_system_ctrl_airbag(system, THIGH_AIRBAG_IDX_0, AIRBAG_STATE_KEEP);
        break;
    }
}

/**
 * @brief 验证腿部系统运行模式
 *
 * @param   mode         运行模式
 * @return  result_e     验证结果, 验证成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
static result_e app_thigh_validate_mode(uint32_t mode)
{
    thigh_mode_e thigh_mode = (thigh_mode_e)mode;
    if (thigh_mode < THIGH_MODE_UP || thigh_mode >= THIGH_MODE_MAX) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    return RESULT_STATUS_OK;
}
